package com.example.doer.reading.AppActivities;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.doer.reading.R;

/**
 * Created by 严旭江 on 2017/7/21.
 */

//提问界面 实现图文混排
public class AskQuestionActivity extends AppCompatActivity implements View.OnClickListener {
    Button buttonReturn;
    Button buttonNext;
    EditText questionContent;
    public static String content;//问题详情

    @Override
    public void onCreate(Bundle savedInstancetype) {
        super.onCreate(savedInstancetype);
        setContentView(R.layout.activity_ask_question_1);

        buttonReturn = (Button) findViewById(R.id.askQuestion_btnReturn);
        buttonNext = (Button) findViewById(R.id.askQuestion_btNext);
        questionContent = (EditText) findViewById(R.id.questionContent);
        buttonReturn.setOnClickListener(this);
        buttonNext.setOnClickListener(this);
        //获取焦点
        questionContent.requestFocus();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.askQuestion_btnReturn: {//return Btn
                /**
                 * 响应码2001表示返回
                 */
                AskQuestionActivity.this.setResult(2001);
                AskQuestionActivity.this.finish();
                break;
            }
            case R.id.askQuestion_btNext: {
                content = questionContent.getText().toString();
                if (!TextUtils.isEmpty(content.trim())) {
                    if (content.length() < 10) {
                        Toast.makeText(AskQuestionActivity.this, "Please Imput more", Toast.LENGTH_SHORT).show();
                        break;
                    }
                    if (content.trim().endsWith("?") || content.trim().endsWith("？")) {
                        /**
                         * 传递s为问题（第二级）name
                         */
                        Intent intent = new Intent(AskQuestionActivity.this, AskQuestionTwoActivity.class);
                        startActivityForResult(intent, 1001);
                    } else
                        Toast.makeText(AskQuestionActivity.this, "please end with ?", Toast.LENGTH_SHORT).show();
                } else
                    Toast.makeText(AskQuestionActivity.this, "Input Cannot Be NULL", Toast.LENGTH_SHORT).show();
                break;
            }
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        //成功
        if (resultCode == 2002) {
            AskQuestionActivity.this.setResult(2002);
            AskQuestionActivity.this.finish();
        }
        if (resultCode == 2001) {//返回
            //NULL操作
            questionContent.setText(content);
        }
    }
}